*****************************************************************************
	*** Figure 4 ***
******************************************************************************
	
	//Use main data
	use "${maindta}", clear

*Get quartiles
preserve
collapse (mean) firm_score_est (median) median=firm_score_est (mean) vae_rs CPIscore2017 [pw=wt], by(wbcode)
su median , d
gen 	quartile=1 if median<=`r(p25)'
replace quartile=2 if median>`r(p25)' & median<`r(p75)'
replace quartile=3 if median>=`r(p75)' & !missing(median)

gsort median 
gen rank=_n

gsort vae_rs
gen vrank=_n

gsort CPIscore2017
gen crank=_n

tempfile quartiles 
save `quartiles'
restore 

merge wbcode using `quartiles' , sort uniqusing 


global col1 "245 200 75"
global col2 "138 47  99"
global col3 "102 3 115"

gen x1=.
gen d0=.
gen ul=.
gen ll=.

forval j=1/41 {
    cap drop sort where _x1
	qui levelsof wbcode  	if rank==`j', loc(wb) clean
	qui levelsof quartile 	if rank==`j', loc(q) clean
	gen sort=1 if wbcode=="`wb'"
	sort sort     
    gen where = _n + 4 in 1/45 
	local choices bwidth(1)
	di "`wb'"
	kdensity firm_score_est if wbcode=="`wb'" [aw=wt], `choices' gen(_x1 d1`wb') nodraw
	replace d0=`j'*.05 					if sort==1 
	replace d1`wb'=d1`wb'+(`j'*.05)  	if sort==1
	replace x1=_x1 				if sort==1
	qui su d0 if wbcode=="`wb'" , d
	replace ll=`r(min)' if wbcode=="`wb'" 
	qui su d1`wb' if inrange(firm_score_est,median*.96,median*1.04) & wbcode=="`wb'" 
	replace ul=`r(max)' if wbcode=="`wb'" 
}
sort wbcode x1
twoway ///
	(rarea d1HRV d0 x1, color("$col3%75"))  /// 
	(rarea d1LBN d0 x1, color("$col3%75")) /// 
	(rarea d1TUR d0 x1, color("$col3%75")) /// 
	(rarea d1PSE d0 x1, color("$col3%75")) /// 
	(rarea d1HUN d0 x1, color("$col3%75")) /// 
	(rarea d1JOR d0 x1, color("$col3%75")) /// 
	(rarea d1GRC d0 x1, color("$col3%75")) /// 
	(rarea d1TUN d0 x1, color("$col3%75")) /// 
	(rarea d1EGY d0 x1, color("$col3%75")) /// 
	(rarea d1MNG d0 x1, color("$col3%75")) /// 
	(rarea d1SRB d0 x1, color("$col3%75")) /// 
	(rarea d1SVN d0 x1, color("$col2%40")) /// 
	(rarea d1CYP d0 x1, color("$col2%40")) /// 
	(rarea d1BIH d0 x1, color("$col2%40")) /// 
	(rarea d1PRT d0 x1, color("$col2%40")) /// 
	(rarea d1MNE d0 x1, color("$col2%40")) /// 
	(rarea d1LVA d0 x1, color("$col2%40")) /// 
	(rarea d1MLT d0 x1, color("$col2%40")) /// 
	(rarea d1EST d0 x1, color("$col2%40")) /// 
	(rarea d1MKD d0 x1, color("$col2%40")) /// 
	(rarea d1XKX d0 x1, color("$col2%40")) /// 
	(rarea d1CZE d0 x1, color("$col2%40")) /// 
	(rarea d1ITA d0 x1, color("$col2%40")) /// 
	(rarea d1KGZ d0 x1, color("$col2%40")) /// 
	(rarea d1MDA d0 x1, color("$col2%40")) /// 
	(rarea d1UZB d0 x1, color("$col2%40")) /// 
	(rarea d1ROU d0 x1, color("$col2%40")) /// 
	(rarea d1TJK d0 x1, color("$col2%40")) /// 
	(rarea d1BGR d0 x1, color("$col2%40")) /// 
	(rarea d1SVK d0 x1, color("$col2%40")) /// 
	(rarea d1ALB d0 x1, color("$col2%40")) /// 
	(rarea d1KAZ d0 x1, color("$col1%40")) /// 
	(rarea d1BLR d0 x1, color("$col1%40")) /// 
	(rarea d1ARM d0 x1, color("$col1%40")) /// 
	(rarea d1LTU d0 x1, color("$col1%40")) /// 
	(rarea d1RUS d0 x1, color("$col1%40")) /// 
	(rarea d1GEO d0 x1, color("$col1%40")) /// 
	(rarea d1POL d0 x1, color("$col1%40")) /// 
	(rarea d1MAR d0 x1, color("$col1%40")) /// 
	(rarea d1UKR d0 x1, color("$col1%40")) /// 
	(rarea d1AZE d0 x1, color("$col1%40")) /// 
	(rspike ul ll median if quartile==1, lcolor("$col1%50"))				   ///
	(rspike ul ll median if quartile==2, lcolor("$col2%50"))				   ///
	(rspike ul ll median if quartile==3, lcolor("$col3%50"))				   ///
	, xlab(0 25 50 75 100, nogrid notick) xsize(4) ysize(6) ylabel(0.085 "AZE"	0.135 "UKR"	0.185 "MAR"	0.235 "POL"	0.285 "LTU"	0.335 "GEO"	0.385 "ARM"	0.435 "RUS"	0.485 "BLR"	0.535 "KAZ"	0.585 "SVK"	0.635 "ALB"	0.685 "BGR"	0.735 "ROU"	0.785 "TJK"	0.835 "UZB"	0.9 "MDA"	0.95 "KGZ"	1 "ITA"	1.05 "CZE"	1.1 "XKX"	1.15 "MKD"	1.2 "EST"	1.25 "MLT"	1.3 "MNE"	1.35 "LVA"	1.4 "PRT"	1.45 "BIH"	1.5 "CYP"	1.55 "SVN"	1.6 "SRB"	1.65 "MNG"	1.7 "TUN"	1.75 "EGY"	1.8 "GRC"	1.85 "JOR"	1.9 "HUN"	1.95 "PSE"	2 "TUR"	2.05 "LBN"	2.1 "HRV", nogrid notick ) yscale(lstyle(none)) ///
	legend(order(1 "Highest quartile" 24 "Middle quartiles" 41 "Lowest quartile") ring(0) pos(5) size(small)) xtitle("Political Influence Score (0 to 100)")
		
	graph export "${outfigures}\Figure4.pdf", replace
	